﻿<?php
session_start();
require_once "config.php";
$chuthuong='abcdefghijklmnopqrstuvwxyz';
$chuhoa='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$chuso='1234567890';
$chucai=$chuthuong.$chuhoa;
?>
<html>
<head>
<title>Công việc</title>
<meta  http-equiv="content-type" content="text/html; charset=utf-8" />
<meta  name="keywords" content="công việc,việc,kế hoạch" />
<meta  name="author" content="Trần Hữu Nam - huunam0@gmail.com" />
<script src="inc/jquery.min.js" type="text/javascript"></script>
<link rel=stylesheet href='inc/mystyle.css' type='text/css'>
<script type="text/javascript">

$(document).ready(function(){
	$(".chucnang").hide();
	$(".congviec").on({mouseenter:function(){
		$(this).find(".chucnang").show(500);
	},mouseleave:function(){
		$(this).find(".chucnang").hide();
	}});
	$(".hoanthanh").click(function(){
		var maso=$(this).parent().parent().find(".maso").text();
		$.post("finish.php",{viec:maso});
		$(this).parent().parent().hide();
	});
	$(".giahan").click(function(){
		var maso=$(this).parent().parent().find(".maso").text();
		$.post("renew.php",{viec:maso});
	});
});
</script>
</head>
<body>
<?php
if (isset($_GET['m'])) {
	$mat = retain($_GET['m'],$chucai.$chuso);
	$sql="SELECT  viec.ndung,viec.trang from users,viec where users.id=viec.user and users.trang=1 and ".(isset($_GET['all'])?"":" viec.trang=1 and ")." users.mat='$mat'";
	echo "<div id='phanviec'>";
	$result = mysql_query($sql);
	$i=1;
	while ($data=mysql_fetch_array($result)) {
		echo "<div class='congviec'>";
		echo "<span class='thutu'>".$i."</span>";
		echo "<span class='noidung'>".$data['ndung']."</span>";
		echo "</div>";
		$i++;
	}
	echo "</div>";
	echo "<hr/><div id='themviec'><form action='add.php' method='post'>";
	echo "Thêm công việc: <input type='text' name='newtask' id='newtask' size='50'/><input type='hidden' name='m' value='$mat'/><input type='submit' name='themmoi' value='Thêm'/>";
	echo "</form></div>";
	echo "<br/><a href='login.php'>Login</a>";
} else {
	if (isset($_SESSION['name'])) {
		if (isset($_POST['themmoi'])) {
			
		} else {
			$uname=$_SESSION['name'];
			$sql="SELECT  viec.* from users,viec where users.id=viec.user and users.trang=1 and ".(isset($_GET['all'])?"":" viec.trang=1 and ")." users.name='$uname'";
			$result = mysql_query($sql);
			$i=1;
			echo "<div id='phanviec'>";
			while ($data=mysql_fetch_array($result)) {
				echo "<div class='congviec'>";
				echo "<span class='thutu'>".$i."</span>";
				echo "<span class='maso'>".$data['id']."</span>";
				echo "<span class='noidung'>".$data['ndung']."</span>";
				echo "<span class='chucnang'>";
				if ($data['trang'])
					echo "<span class='hoanthanh button'>Hoàn thành</span>";
				else
					echo "<span class='giahan button'>Gia hạn</span>";
				echo "</span></div>";
				$i++;
			}
			echo "</div>";
			echo "<hr/><div id='themviec'><form action='add.php' method='post'>";
			echo "Thêm công việc: <input type='text' name='newtask' id='newtask' size='50'/><input type='submit' name='themmoi' value='Thêm'/>";
			echo "<span id='addtask'>Thêm</span></form></div>";
		}
		echo "<br/><a href='logout.php'>Logout</a>";
	} else {
		echo "<br/><a href='login.php'>Login</a>";
	}
}

function retain($str,$kitu) {
	$len = strlen($str);
	$sret="";
	for ($i=0; $i<$len; $i++) {
		if (strpos($kitu,substr($str,$i,1))!==false) 
			$sret .= substr($str,$i,1);
	}
	return $sret;
}
function redirect($location, $delaytime = 0) {
    if ($delaytime>0) {    
        header( "refresh: $delaytime; url='".str_replace("&amp;", "&", $location)."'" );
    } else {
        header("Location: ".str_replace("&amp;", "&", $location));
    }    
}
?>
